<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>13.Python综合案例-动态柱状图 | 扣小妖</title><meta name="author" content="扣小妖"><meta name="copyright" content="扣小妖"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python基础综合案例 数据可视化 - 动态柱状图 章节目录 [!WARNING|label:本章需要学习的内容]   基础柱状图 基础时间线柱状图 GDP动态柱状图绘制  练习的数据源1960-2019全球GDP数据.csv1960-2019全球GDP数据.csv.zip 基础柱状图 视频学习       PPT资料     代码演示  123456789101112131415161718#">
<meta property="og:type" content="article">
<meta property="og:title" content="13.Python综合案例-动态柱状图">
<meta property="og:url" content="http://liukuaizhuan.gitee.io/posts/18141c11.html">
<meta property="og:site_name" content="扣小妖">
<meta property="og:description" content="Python基础综合案例 数据可视化 - 动态柱状图 章节目录 [!WARNING|label:本章需要学习的内容]   基础柱状图 基础时间线柱状图 GDP动态柱状图绘制  练习的数据源1960-2019全球GDP数据.csv1960-2019全球GDP数据.csv.zip 基础柱状图 视频学习       PPT资料     代码演示  123456789101112131415161718#">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/4f58efcc75f80e499f83b69e6a40f221.png">
<meta property="article:published_time" content="2022-12-08T12:04:06.000Z">
<meta property="article:modified_time" content="2022-12-08T12:04:25.882Z">
<meta property="article:author" content="扣小妖">
<meta property="article:tag" content="笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/4f58efcc75f80e499f83b69e6a40f221.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://liukuaizhuan.gitee.io/posts/18141c11"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '13.Python综合案例-动态柱状图',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-12-08 20:04:25'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/4f58efcc75f80e499f83b69e6a40f221.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">扣小妖</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">13.Python综合案例-动态柱状图</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2022-12-08T12:04:06.000Z" title="发表于 2022-12-08 20:04:06">2022-12-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python%E5%9F%BA%E7%A1%80/">python基础</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="13.Python综合案例-动态柱状图"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python基础综合案例-数据可视化-动态柱状图"><a href="#Python基础综合案例-数据可视化-动态柱状图" class="headerlink" title="Python基础综合案例 数据可视化 - 动态柱状图"></a>Python基础综合案例 数据可视化 - 动态柱状图</h1><hr>
<h2 id="章节目录"><a href="#章节目录" class="headerlink" title="章节目录"></a><strong>章节目录</strong></h2><blockquote>
<p>[!WARNING|label:本章需要学习的内容]</p>
</blockquote>
<ul>
<li>基础柱状图</li>
<li>基础时间线柱状图</li>
<li>GDP动态柱状图绘制</li>
</ul>
<h2 id="练习的数据源"><a href="#练习的数据源" class="headerlink" title="练习的数据源"></a><strong>练习的数据源</strong></h2><p><a target="_blank" rel="noopener" href="https://sharewh1.xuexi365.com/share/download/ffee02d8a9e39288dea5badc912e165d">1960-2019全球GDP数据.csv</a><br><a target="_blank" rel="noopener" href="https://sharewh1.xuexi365.com/share/download/1d54ae9a38254d85f78ab906c52bf263">1960-2019全球GDP数据.csv.zip</a></p>
<h2 id="基础柱状图"><a href="#基础柱状图" class="headerlink" title="基础柱状图"></a><strong>基础柱状图</strong></h2><blockquote>
<p>视频学习 </p>
</blockquote>
<iframe src="//player.bilibili.com/player.html?aid=941747210&bvid=BV1qW4y1a7fU&cid=818139026&page=108" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" height="500"> </iframe>

<blockquote>
<p>PPT资料</p>
</blockquote>
<p><img src="https://p.ananas.chaoxing.com/star3/origin/205bc0cbee6447451871f2390359d48e.png" alt="1"><br><img src="https://p.ananas.chaoxing.com/star3/origin/e91cf8863937b568ab896a6d25944ab7.png" alt="2"><br><img src="https://p.ananas.chaoxing.com/star3/origin/3959a3b4da0f80c5508ccb3ace0f9ebb.png" alt="3"><br><img src="https://p.ananas.chaoxing.com/star3/origin/056f4ed2ad170a0608098c1a6fc0e333.png" alt="4"><br><img src="https://p.ananas.chaoxing.com/star3/origin/4e19f00be92b3e2530969486dc08f0bd.png" alt="5"> </p>
<blockquote>
<p>代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 柱状图基础 </span></span><br><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Bar</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> LabelOpts</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用Bar 构建基础柱状图</span></span><br><span class="line">bar = Bar()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加x轴数据</span></span><br><span class="line">bar.add_xaxis([<span class="string">&quot;中国&quot;</span>, <span class="string">&quot;美国&quot;</span>, <span class="string">&quot;英国&quot;</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加Y轴数据</span></span><br><span class="line">bar.add_yaxis(<span class="string">&quot;GDP&quot;</span>, [<span class="number">30</span>, <span class="number">20</span>, <span class="number">10</span>], label_opts=LabelOpts(position=<span class="string">&quot;right&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 反转xy轴</span></span><br><span class="line">bar.reversal_axis()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 绘图</span></span><br><span class="line">bar.render() </span><br></pre></td></tr></table></figure>

<h2 id="基础时间线柱状图"><a href="#基础时间线柱状图" class="headerlink" title="基础时间线柱状图"></a><strong>基础时间线柱状图</strong></h2><blockquote>
<p>视频学习</p>
</blockquote>
<iframe src="//player.bilibili.com/player.html?aid=941747210&bvid=BV1qW4y1a7fU&cid=818143033&page=109" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" height='500'> </iframe>

<blockquote>
<p>PPT 资料</p>
</blockquote>
<p><img src="https://p.ananas.chaoxing.com/star3/origin/55f392fd71327282b263f1597ee1c02a.png" alt="1"><br><img src="https://p.ananas.chaoxing.com/star3/origin/93323eccb9a2d02264b764015019d824.png" alt="2"><br><img src="https://p.ananas.chaoxing.com/star3/origin/05e3f62600bbcd402c9360007f64ec4d.png" alt="3"><br><img src="https://p.ananas.chaoxing.com/star3/origin/70f106b3fdef9701397cea4d7144ab19.png" alt="4"><br><img src="https://p.ananas.chaoxing.com/star3/origin/637808b348a77c63f4ee4729786f121c.png" alt="5"><br><img src="https://p.ananas.chaoxing.com/star3/origin/aea25301702426f100144c2100194da3.png" alt="6"></p>
<blockquote>
<p>代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 基础时间线</span></span><br><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Bar, Timeline</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用Bar 构建基础柱状图 1</span></span><br><span class="line">bar1 = Bar()</span><br><span class="line">bar1.add_xaxis([<span class="string">&quot;中国&quot;</span>, <span class="string">&quot;美国&quot;</span>, <span class="string">&quot;英国&quot;</span>])</span><br><span class="line">bar1.add_yaxis(<span class="string">&quot;GDP&quot;</span>, [<span class="number">30</span>, <span class="number">20</span>, <span class="number">10</span>], label_opts=LabelOpts(position=<span class="string">&quot;right&quot;</span>))</span><br><span class="line">bar1.reversal_axis()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用Bar 构建基础柱状图 2</span></span><br><span class="line">bar2 = Bar()</span><br><span class="line">bar2.add_xaxis([<span class="string">&quot;中国&quot;</span>, <span class="string">&quot;美国&quot;</span>, <span class="string">&quot;英国&quot;</span>])</span><br><span class="line">bar2.add_yaxis(<span class="string">&quot;GDP&quot;</span>, [<span class="number">130</span>, <span class="number">120</span>, <span class="number">110</span>], label_opts=LabelOpts(position=<span class="string">&quot;right&quot;</span>))</span><br><span class="line">bar2.reversal_axis()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用Bar 构建基础柱状图 3</span></span><br><span class="line">bar3 = Bar()</span><br><span class="line">bar3.add_xaxis([<span class="string">&quot;中国&quot;</span>, <span class="string">&quot;美国&quot;</span>, <span class="string">&quot;英国&quot;</span>])</span><br><span class="line">bar3.add_yaxis(<span class="string">&quot;GDP&quot;</span>, [<span class="number">230</span>, <span class="number">220</span>, <span class="number">120</span>], label_opts=LabelOpts(position=<span class="string">&quot;right&quot;</span>))</span><br><span class="line">bar3.reversal_axis()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 构建时间线对象，使用主题需要先导包</span></span><br><span class="line"><span class="keyword">from</span> pyecharts.<span class="built_in">globals</span> <span class="keyword">import</span> ThemeType</span><br><span class="line">timeline = Timeline(</span><br><span class="line">    <span class="comment"># 主题设置</span></span><br><span class="line">    &#123;<span class="string">&quot;theme&quot;</span>:ThemeType.LIGHT&#125; <span class="comment">#蓝黄粉颜色主题</span></span><br><span class="line">)</span><br><span class="line"><span class="comment"># 在时间线内添加柱状图对象</span></span><br><span class="line">timeline.add(bar1, <span class="string">&#x27;点1&#x27;</span>)</span><br><span class="line">timeline.add(bar2, <span class="string">&#x27;点2&#x27;</span>)</span><br><span class="line">timeline.add(bar3, <span class="string">&#x27;点3&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 时间线设置模式架构</span></span><br><span class="line">timeline.add_schema(</span><br><span class="line">    play_interval=<span class="number">1000</span>,     <span class="comment"># 自动播放的时间间隔，单位毫秒</span></span><br><span class="line">    is_timeline_show=<span class="literal">True</span>,  <span class="comment"># 是否在自动播放的时候显示时间线</span></span><br><span class="line">    is_auto_play=<span class="literal">True</span>,      <span class="comment"># 是否自动播放</span></span><br><span class="line">    is_loop_play=<span class="literal">True</span>       <span class="comment"># 是否循环自动播放</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 绘图，用的是时间线对象绘图，而不是用bar对象</span></span><br><span class="line">timeline.render(<span class="string">&quot;基础时间线柱状图.html&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="GDP动态柱状图绘制"><a href="#GDP动态柱状图绘制" class="headerlink" title="GDP动态柱状图绘制"></a><strong>GDP动态柱状图绘制</strong></h2><blockquote>
<p>学习视频</p>
</blockquote>
<iframe src="//player.bilibili.com/player.html?aid=941747210&bvid=BV1qW4y1a7fU&cid=818160954&page=110" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" height="500"> </iframe>

<blockquote>
<p>PPT资料</p>
</blockquote>
<p><img src="https://p.ananas.chaoxing.com/star3/origin/ba41eb3f99d48ba1119933e48650045c.png" alt="1"><br><img src="https://p.ananas.chaoxing.com/star3/origin/690a5296b40bac7ca30a117e3325e617.png" alt="2"><br><img src="https://p.ananas.chaoxing.com/star3/origin/5f35efb159ce5f4a7d6fd01c31008a99.png" alt="3"><br><img src="https://p.ananas.chaoxing.com/star3/origin/9a3d0fb1584b142e95f2f1fc202ca073.png" alt="4"><br><img src="https://p.ananas.chaoxing.com/star3/origin/8c66d70b115445ecf56d0b8cc433dc65.png" alt="5"><br><img src="https://p.ananas.chaoxing.com/star3/origin/57dce5724e964a51fb3acec2e29ff348.png" alt="6"><br><img src="https://p.ananas.chaoxing.com/star3/origin/aa88101006c308f90b4bee545b1d2c6c.png" alt="7"><br><img src="https://p.ananas.chaoxing.com/star3/origin/3aa2d0d044fbd1102c6b19aa3a0a9fa6.png" alt="8"></p>
<blockquote>
<p>扩展列表sort方法 代码演示</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 扩展列表sort方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 准备列表</span></span><br><span class="line">my_list = [[<span class="string">&quot;a&quot;</span>, <span class="number">33</span>], [<span class="string">&quot;b&quot;</span>, <span class="number">55</span>], [<span class="string">&quot;c&quot;</span>, <span class="number">11</span>]]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过函数 让第一个值作为依据</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">choose_sort_key</span>(<span class="params">element</span>):</span><br><span class="line">    <span class="keyword">return</span> element[<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 排序，基于待命函数</span></span><br><span class="line"><span class="comment"># key 通过函数，作为依据</span></span><br><span class="line">my_list.sort(key=choose_sort_key, reverse=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># lambda匿名函数表达式</span></span><br><span class="line">my_list.sort(key=<span class="keyword">lambda</span> element:element[<span class="number">1</span>],reverse=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(my_list)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>代码演示 GDP动态柱状图开发</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># GDP动态柱状图开发</span></span><br><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Bar, Timeline</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">from</span> pyecharts.<span class="built_in">globals</span> <span class="keyword">import</span> ThemeType</span><br><span class="line">pathFile = <span class="string">&#x27;/Volumes/E盘/资料/可视化案例数据/动态柱状图数据/1960-2019全球GDP数据.csv&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取文件内容,并关闭文件</span></span><br><span class="line">f = <span class="built_in">open</span>(pathFile, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;GB2312&#x27;</span>)</span><br><span class="line">data_lines = f.readlines()</span><br><span class="line">f.close()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除第一条无用的数据</span></span><br><span class="line">data_lines.pop(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将数据转换为字典存储</span></span><br><span class="line"><span class="comment"># &#123; 年份：[[国家,GDP],[国家,GDP],[国家,GDP],[国家,GDP]....],2021:[[国家,GDP],[国家,GDP],[国家,GDP],[国家,GDP]....]&#125;</span></span><br><span class="line"><span class="comment">#  定义一个空字典</span></span><br><span class="line">data_dict = &#123;&#125;</span><br><span class="line"><span class="comment"># 构建时间线对象</span></span><br><span class="line">timeline = Timeline(</span><br><span class="line">    <span class="comment"># 主题设置</span></span><br><span class="line">    &#123;<span class="string">&quot;theme&quot;</span>:ThemeType.LIGHT&#125; <span class="comment">#蓝黄粉颜色主题</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> data_lines:</span><br><span class="line">    year = <span class="built_in">int</span>(line.split(<span class="string">&quot;,&quot;</span>)[<span class="number">0</span>])  <span class="comment"># 年份</span></span><br><span class="line">    country = line.split(<span class="string">&quot;,&quot;</span>)[<span class="number">1</span>]  <span class="comment"># 国家</span></span><br><span class="line">    gdp = <span class="built_in">float</span>(line.split(<span class="string">&quot;,&quot;</span>)[<span class="number">2</span>])  <span class="comment"># gdp数据</span></span><br><span class="line">    <span class="comment"># 如何判断字典里面有没有指定的key呢？</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        data_dict[year].append([country, gdp])</span><br><span class="line">    <span class="keyword">except</span> KeyError:</span><br><span class="line">        data_dict[year] = []</span><br><span class="line">        data_dict[year].append([country, gdp])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 排序年限 升序</span></span><br><span class="line">sorted_year_list = <span class="built_in">sorted</span>(data_dict.keys())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 只需要前八个国家的数据 ，对列表进行排序</span></span><br><span class="line"><span class="keyword">for</span> year <span class="keyword">in</span> sorted_year_list:</span><br><span class="line">    <span class="comment"># GDP进行降序</span></span><br><span class="line">    data_dict[year].sort(key=<span class="keyword">lambda</span> element: element[<span class="number">1</span>], reverse=<span class="literal">True</span>)</span><br><span class="line">    <span class="comment"># 取出本年份前8名的国家</span></span><br><span class="line">    year_data = data_dict[year][<span class="number">0</span>:<span class="number">8</span>]</span><br><span class="line">    <span class="comment"># 构建数据轴</span></span><br><span class="line">    x_data = []</span><br><span class="line">    y_data = []</span><br><span class="line">    <span class="keyword">for</span> country_gdp <span class="keyword">in</span> year_data:</span><br><span class="line">        x_data.append(country_gdp[<span class="number">0</span>])  <span class="comment"># x轴添加国家</span></span><br><span class="line">        y_data.append( country_gdp[<span class="number">1</span>] / <span class="number">100000000</span> )  <span class="comment"># y轴添加GDP数据，单位为：亿元</span></span><br><span class="line">    <span class="comment"># 构建柱状图</span></span><br><span class="line">    bar = Bar()</span><br><span class="line">    <span class="comment"># 数据进行反转一下，柱状图 GDP高的在上面，GDP低的在下面</span></span><br><span class="line">    x_data.reverse()</span><br><span class="line">    y_data.reverse()</span><br><span class="line"></span><br><span class="line">    bar.add_xaxis(x_data)</span><br><span class="line">    bar.add_yaxis(<span class="string">&quot;GDP(亿元)&quot;</span>, y_data, label_opts=LabelOpts(position=<span class="string">&quot;right&quot;</span>))</span><br><span class="line">    <span class="comment"># 反转 x轴 和 y轴</span></span><br><span class="line">    bar.reversal_axis()</span><br><span class="line">    <span class="comment"># 设置每一年的标题</span></span><br><span class="line">    bar.set_global_opts(</span><br><span class="line">        title_opts=TitleOpts(title=<span class="string">f&quot;<span class="subst">&#123;year&#125;</span>年全球前8GDP&quot;</span>)</span><br><span class="line">    )</span><br><span class="line">    <span class="comment"># 添加到时间线</span></span><br><span class="line">    timeline.add(bar, <span class="built_in">str</span>(year) + <span class="string">&quot;年&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置时间线自动播放</span></span><br><span class="line">timeline.add_schema(</span><br><span class="line">    play_interval=<span class="number">500</span>,</span><br><span class="line">    is_auto_play=<span class="literal">True</span>,</span><br><span class="line">    is_loop_play=<span class="literal">False</span>,</span><br><span class="line">    is_timeline_show=<span class="literal">True</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最后绘图</span></span><br><span class="line">timeline.render(<span class="string">&quot;1960-2019全球GDP前8个国家.html&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io">扣小妖</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io/posts/18141c11.html">http://liukuaizhuan.gitee.io/posts/18141c11.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本站所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/4f58efcc75f80e499f83b69e6a40f221.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://lib.baomitu.com/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" alt="微信打赏"/></a><div class="post-qr-code-desc">微信打赏</div></li><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" alt="支付宝打赏"/></a><div class="post-qr-code-desc">支付宝打赏</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/6ebaa02b.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/d24d9bfcd4ce3f910d6b5cdc78676d75.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">14.Python面向对象</div></div></a></div><div class="next-post pull-right"><a href="/posts/e46ce5f3.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/05fcdda2bab7b7f1768a65f0c49762f8.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">12.Python综合案例-地图可视化</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">扣小妖</div><div class="author-info__description">Email：  liukuaizhuan@qq.com</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%9F%BA%E7%A1%80%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B-%E6%95%B0%E6%8D%AE%E5%8F%AF%E8%A7%86%E5%8C%96-%E5%8A%A8%E6%80%81%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">1.</span> <span class="toc-text">Python基础综合案例 数据可视化 - 动态柱状图</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AB%A0%E8%8A%82%E7%9B%AE%E5%BD%95"><span class="toc-number">1.1.</span> <span class="toc-text">章节目录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%83%E4%B9%A0%E7%9A%84%E6%95%B0%E6%8D%AE%E6%BA%90"><span class="toc-number">1.2.</span> <span class="toc-text">练习的数据源</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">1.3.</span> <span class="toc-text">基础柱状图</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E6%97%B6%E9%97%B4%E7%BA%BF%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">1.4.</span> <span class="toc-text">基础时间线柱状图</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#GDP%E5%8A%A8%E6%80%81%E6%9F%B1%E7%8A%B6%E5%9B%BE%E7%BB%98%E5%88%B6"><span class="toc-number">1.5.</span> <span class="toc-text">GDP动态柱状图绘制</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象"><img src="https://p.ananas.chaoxing.com/star3/origin/7ea674ffcd12ef7d943d54c93e9203a4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="013.c++核心编程-类和对象"/></a><div class="content"><a class="title" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象">013.c++核心编程-类和对象</a><time datetime="2023-02-13T14:55:03.000Z" title="发表于 2023-02-13 22:55:03">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高"><img src="https://p.ananas.chaoxing.com/star3/origin/b6c49bb4892ad191edbc0363caf45530.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="012.C++核心编程-函数提高"/></a><div class="content"><a class="title" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高">012.C++核心编程-函数提高</a><time datetime="2023-02-13T14:51:24.000Z" title="发表于 2023-02-13 22:51:24">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/bff12c11.html" title="011.C++核心编程-引用"><img src="https://p.ananas.chaoxing.com/star3/origin/86403540a7b572fec123890e9a638adc.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="011.C++核心编程-引用"/></a><div class="content"><a class="title" href="/posts/bff12c11.html" title="011.C++核心编程-引用">011.C++核心编程-引用</a><time datetime="2023-02-13T14:47:34.000Z" title="发表于 2023-02-13 22:47:34">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型"><img src="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="010.C++核心编程-内存分区模型"/></a><div class="content"><a class="title" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型">010.C++核心编程-内存分区模型</a><time datetime="2023-02-06T15:28:05.000Z" title="发表于 2023-02-06 23:28:05">2023-02-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统"><img src="https://p.ananas.chaoxing.com/star3/origin/5cdbcc7b75aea2aa3a8b12cae1439e99.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="009.C++基础实战-通讯录管理系统"/></a><div class="content"><a class="title" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统">009.C++基础实战-通讯录管理系统</a><time datetime="2023-02-04T10:02:31.000Z" title="发表于 2023-02-04 18:02:31">2023-02-04</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 扣小妖</div><div class="footer_custom_text"><p style="margin:10px 0 5px"><a target="_blank" href="https://hexo.io/" style="margin-inline:5px">博客 hexo</a><a target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px">主题 Butterfly</a><a target="_blank" href="https://gitee.com/" style="margin-inline:5px">托管 Gitee</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://gcore.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://gcore.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>